package com.whisperarts.financer.app.views;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.ViewPart;

import com.whisperarts.financer.app.actions.AddCategoryAction;
import com.whisperarts.financer.app.actions.DeleteCategoryAction;
import com.whisperarts.financer.app.actions.EditCategoryAction;
import com.whisperarts.financer.app.model.rcp.CategoriesContentProvider;
import com.whisperarts.financer.app.model.rcp.CategoriesLabelProvider;

public class CategoryView extends ViewPart {

	public static final String ID = "com.whisperarts.financer.app.views.category";

	private static TreeViewer viewer;

	private FormToolkit toolkit;
    private Form form;
	
	@Override
	public void createPartControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		
		form = toolkit.createForm(parent);
        
        toolkit.paintBordersFor(form.getBody());
        
        form.getBody().setLayout(parent.getLayout());
        
		viewer = new TreeViewer(form.getBody(), SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER);
		
		
		viewer.setAutoExpandLevel(3);		
		viewer.setContentProvider(new CategoriesContentProvider());
		viewer.setLabelProvider(new CategoriesLabelProvider());
		viewer.setInput(new Object());
		viewer.setData(FormToolkit.KEY_DRAW_BORDER,
                FormToolkit.TEXT_BORDER);
		
		getSite().setSelectionProvider(viewer);
		
		getViewSite().getActionBars().getMenuManager().add(new AddCategoryAction(getViewSite().getWorkbenchWindow()));
		getViewSite().getActionBars().getMenuManager().add(new EditCategoryAction(getViewSite().getWorkbenchWindow()));
		getViewSite().getActionBars().getMenuManager().add(new DeleteCategoryAction(getViewSite().getWorkbenchWindow()));
	}

	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
	}

	public static TreeViewer getViewer() {
		return viewer;
	}

}
